package com.xneure.enging;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Map;
import java.util.Vector;
import java.util.concurrent.ConcurrentHashMap;
import com.xneure.enging.event.EventNotify;
import com.xneure.enging.event.Notify;
import com.xneure.enging.event.Notify.NotifyType;
import com.xneure.enging.ui.UiElement;
import com.xneure.utils.bean.GUID;

public class RequestContext{
	private static Map<String,Collection<Notify>> notifys;
	private static ThreadLocal<Map<String,Object>> currRequest = new ThreadLocal<Map<String,Object>>();
	private static EventNotify en = new EventNotify(){
		@Override
		public void activate(NotifyType type,String name,Object val,GUID target){
			if(null != event){
				EventNotify e = event.get(WebContext.getSession().getId());
				if(null != e){
					e.activate(type,name,val,target);
				}
			}
		}
	};
	private static Map<String,EventNotify> event;

	public static void setParam(Map<String,Object> reqParam){
		currRequest.set(reqParam);
	}

	public static Map<String,Object> getReqParam(){
		return currRequest.get();
	}

	public static Object getReqParam(String parmName){
		return currRequest.get().get(parmName);
	}

	@SuppressWarnings("unchecked")
	public static void register(Notify en){
		synchronized(RequestContext.class){
			if(null == notifys){
				@SuppressWarnings("rawtypes")
				Map map = new ConcurrentHashMap<String,Collection<Notify>>();
				notifys = map;
			}
		}
		String id = WebContext.getSession().getId();
		Collection<Notify> list = notifys.get(id);
		if(list == null){
			list = new Vector<Notify>();
			notifys.put(id,list);
		}
		list.add(en);
		en.setEvent(RequestContext.en);
	}

	@SuppressWarnings("unchecked")
	private static void registerNotifyEvent(String sesid,EventNotify en){
		synchronized(RequestContext.class){
			if(null == event){
				@SuppressWarnings("rawtypes")
				Map map = new ConcurrentHashMap<String,EventNotify>();
				event = map;
			}
		}
		event.put(sesid,en);
	}

	public static EventNotify getEventNotify(String id){
		if(null == event || event.containsKey(id) == false){
			registerNotifyEvent(id,new EventNotify(){
				@Override
				public void activate(NotifyType type,String name,Object val,GUID target){
					if(type == NotifyType.ADD){
						if(val instanceof UiElement){
							UiElement ele = (UiElement)val;
							if(eventRes.containsKey(ele.getId())){
								return;
							}
						}
					}
					trigger(type,name,val,target);
				}
			});
		}
		EventNotify eventNotify = event.get(id);
		return eventNotify;
	}

	public static void destroyNotifyEvent(String sesid){
		if(event.containsKey(sesid)){
			event.remove(sesid);
		}
	}

	public static void clearNotifyEvent(){
		event.get(WebContext.getSession().getId()).initRes();
	}

	public static void destroyNotify(Notify nf){
		Collection<Notify> list = notifys.get(WebContext.getSession().getId());
		notifys.remove(WebContext.getSession().getId());
		ArrayList<Notify> temList = new ArrayList<Notify>(list);
		for(Notify n:temList){
			if(nf == n){
				list.remove(n);
				return;
			}
		}
	}
}
